package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.domain.BTermBrandInfo;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.utils.DateUtil;
import cc.rengu.redp.common.utils.SortUtil;
import cc.rengu.redp.bizimpl.upmp.dao.NuccInstInfoMapper;
import cc.rengu.redp.bizimpl.upmp.domain.NuccInstInfo;
import cc.rengu.redp.bizimpl.upmp.service.NuccInstInfoService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author LiuYuLiang.LL
 */
@Service
@DS("upmp")
public class NuccInstInfoServiceImpl extends ServiceImpl<NuccInstInfoMapper, NuccInstInfo> implements NuccInstInfoService {
    @Autowired
    private IUpmpSysParamService upmpSysParamService;
    @Override
    public IPage<NuccInstInfo> findByPage(QueryRequest request, NuccInstInfo info) {
        try {
            LambdaQueryWrapper<NuccInstInfo> queryWrapper = new LambdaQueryWrapper<>();
            if (StringUtils.isNotBlank(info.getNuccInstId())) {
                queryWrapper.like(NuccInstInfo::getNuccInstId, info.getNuccInstId());
            }
            if (StringUtils.isNotBlank(info.getNuccInstName())) {
                queryWrapper.like(NuccInstInfo::getNuccInstName, info.getNuccInstName());
            }
            queryWrapper.orderByDesc(NuccInstInfo::getCreateTime);
            Page<NuccInstInfo> page = new Page<>();
            SortUtil.handlePageSort(request, page, true);
            return this.page(page, queryWrapper);
        } catch (Exception e) {
            log.error("查询失败", e);
            return null;
        }
    }
    @Override
    @Transactional
    public void create(NuccInstInfo entity) {
        // 获取当前登录用户信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String lastUpddt = DateUtil.getDateFormat(new Date(),DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN);
        entity.setNuccInstStatus(BusinessContainer.data_sta_01);
        entity.setAuditStatus(BusinessContainer.status_02);
        entity.setOprId(user.getId());
        entity.setCrtOrgId(user.getOrgCode());
        entity.setCreateTime(lastUpddt);
        entity.setLastOprId(user.getId());
        entity.setLastUpdateTime(lastUpddt);
        this.baseMapper.insert(entity);
    }
    @Override
    @Transactional
    public void update(NuccInstInfo entity) {
        // 获取当前登录用户信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String lastUpddt = DateUtil.getDateFormat(new Date(),DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN);
        // 修改
        entity.setAuditStatus(BusinessContainer.status_42);
        entity.setLastOprId(user.getId());
        entity.setLastUpdateTime(lastUpddt);
        this.baseMapper.updateById(entity);
    }
    @Override
    @Transactional
    public void cancel(String id) {
        // 注销
        NuccInstInfo entity = this.getById(id);
        entity.setNuccInstStatus(BusinessContainer.data_sta_03);
        entity.setAuditStatus(BusinessContainer.status_12);
        this.baseMapper.updateById(entity);
    }

    @Override
    @Transactional
    public void delete(String[] ids) {
        List<String> list = Arrays.asList(ids);
        this.baseMapper.deleteBatchIds(list);
    }

    @Override
    public Map<String, Object> select2(String q, QueryRequest request) {

        if (null == q) {
            return null;
        }
        q = q.replaceAll("&amp;", "&");

        String brandCodeOrName = "";
        String[] param = q.split("&");
        if (null != param && param.length > 0) {
            brandCodeOrName = param[0];
        }

        try {
            LambdaQueryWrapper<NuccInstInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(NuccInstInfo::getNuccInstStatus, "01");

            if (StringUtils.isNotEmpty(brandCodeOrName)) {
                String finalbrandCodeOrName = brandCodeOrName;
                queryWrapper.and(qw -> qw.likeRight(NuccInstInfo::getNuccInstName, finalbrandCodeOrName).or().likeRight(NuccInstInfo::getNuccInstId, finalbrandCodeOrName));
            }

            Page<NuccInstInfo> page = new Page<>();
            SortUtil.handlePageSort(request, page, true);
            Page<NuccInstInfo> page1 = this.page(page, queryWrapper);
            List<NuccInstInfo> listx = page1.getRecords();
            Map<String, Object> maps = new HashMap<String, Object>();
            Map<String, String> map = new HashMap<String, String>();
            List<Object> list = new ArrayList<Object>();
            if (listx != null) {
                for (int i = 0; i < listx.size(); i++) {
                    NuccInstInfo org = listx.get(i);
                    map = new HashMap<String, String>();
                    map.put("id", org.getNuccInstId());
                    map.put("name", org.getNuccInstName());

                    list.add(map);
                }
                maps.put("results", list);
                maps.put("total", page.getTotal());
                int pageIndex = Common.isBlank(request.getPageNum()) ? 1 : request.getPageNum();
                if (pageIndex == page1.getTotal()) {
                    maps.put("more", false);
                } else {
                    maps.put("more", true);
                }
            }
            return maps;

        } catch (Exception e) {
            log.error("获取网联机构号信息失败", e);
            return null;
        }
    }

}
